$(window).bind("load", function() { 

       var footerHeight = 0,
           footerTop = 0,
           $footer = $("#footer");

       positionFooter();

       function positionFooter() {
                footerHeight = $footer.height();
                footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";

               if ( ($(document.body).height()+footerHeight) < $(window).height()) {
                   $footer.css({
                        position: "absolute", top: footerTop
                   })
               } else {
                   $footer.css({
                        position: "static"
                   })
               }

       }

       $(window)
               .scroll(positionFooter)
               .resize(positionFooter)

});

var selectedOptionsArr = new Array();
var FmasHelper = {
  addToOnLoadEvent : function(func) {
    //Overrides the master onLoad function and adds events to it
    if (typeof window.addEventListener != "undefined") {
      //Mozilla(all), Opera, Safari (latest version)
      window.addEventListener("load", func, false);
    } else if (typeof window.attachEvent != "undefined") {
      //IE 6 and above
      window.attachEvent("onload", func);
    } else {
      //IE 5.5 (?) and below
      if (window.onload != null) {
        var oldOnload = window.onload;
        window.onload = function(e) {
          oldOnload(e);
          func(e);
        };
      } else {
        window.onload = func;
      }
    }
  },
  getById : function(elemId) {
    if (!elemId)return null;
    if (document.all && !document.getElementById) {
      return document.all[elemId];
    } else {
      return document.getElementById(elemId);
    }
  },
  makeSelectOptionsSelectedOnFormSubmit : function(selectBox, currentIndex) {
    if (currentIndex == 0) {
      selectBox.form.currentFormOnSubmit = selectBox.form.onsubmit;
    }
    selectedOptionsArr[currentIndex] = selectBox.options;
    selectBox.form.onsubmit = function() {
      if (selectedOptionsArr) {
        for (var i = 0; i < selectedOptionsArr.length; i++) {
          var tgtList = selectedOptionsArr[i];
          if (tgtList) {
            for (var j = 0; j < tgtList.length; j++) {
              tgtList[j].selected = true;
            }
          }
        }
      }
      if (this.currentFormOnSubmit) {
        return this.currentFormOnSubmit();
      }
    };
  }
};

var instancesOfTransferSelectOptions = new Array();
var TransferSelectOptions = function(sourceIdentifier, targetIdentifier, addToElement) {
  this.currentInstance = instancesOfTransferSelectOptions.length;
  this.sourceToTargetButtonText = "Add";
  this.targetToSourceButtonText = "Remove";
  this.disabledAttributeColor = "#ccc";
  this.disabledAttributeCssClass = null;
  if(!addToElement){
	  document.writeln("<div id='tso" + this.currentInstance + "' class='addRemoveBtn'>");
	  document.writeln('<button class="' + TransferSelectOptions.Util.SOURCE_TO_TARGET + '" type="button">' + this.sourceToTargetButtonText + '</button>');
	  document.writeln('<button class="' + TransferSelectOptions.Util.TARGET_TO_SOURCE + '" type="button">' + this.targetToSourceButtonText + '</button>');
	  document.writeln("</div>");
  }else{
	  var appendVal = "<div id='tso" + this.currentInstance + "' class='addRemoveBtn'>" +
	  		"<button class=\"" + TransferSelectOptions.Util.SOURCE_TO_TARGET + "\" type=\"button\">" + this.sourceToTargetButtonText + "</button>" +
	  		"<button class=\"" + TransferSelectOptions.Util.TARGET_TO_SOURCE + "\" type=\"button\">" + this.targetToSourceButtonText + "</button>" +
	  		"</div>";
	  addToElement.html(appendVal);
  }
  this.container = FmasHelper.getById("tso" + this.currentInstance);
  this.sourceIdentifier = sourceIdentifier;
  this.targetIdentifier = targetIdentifier;
  this.selectTargetOnSubmit = true;
  instancesOfTransferSelectOptions[this.currentInstance] = this;

  var currentInstance = this.currentInstance;
  var transferOnloadInit = function() {
    instancesOfTransferSelectOptions[currentInstance].bindEvent();
  }
  FmasHelper.addToOnLoadEvent(transferOnloadInit);
};

TransferSelectOptions.prototype.setSourceToTargetButtonText = function(sourceToTargetButtonText) {
  this.sourceToTargetButtonText = sourceToTargetButtonText;
};

TransferSelectOptions.prototype.setTargetToSourceButtonText = function(targetToSourceButtonText) {
  this.targetToSourceButtonText = targetToSourceButtonText;
};

TransferSelectOptions.prototype.setDisabledAttributeColor = function(disabledAttributeColor) {
  this.disabledAttributeColor = disabledAttributeColor;
};

TransferSelectOptions.prototype.setDisabledAttributeCssClass = function(disabledAttributeCssClass) {
  this.disabledAttributeCssClass = disabledAttributeCssClass;
};

TransferSelectOptions.prototype.bindUserValues = function() {
  this.buttons.sourceToTargetButton.innerHTML = this.sourceToTargetButtonText;
  this.buttons.targetToSourceButton.innerHTML = this.targetToSourceButtonText;
};

TransferSelectOptions.prototype.bindEvent = function() {
  this.buttons = TransferSelectOptions.Util.getTransferButtons(this.container);
  this.buttons.sourceToTargetButton = this.buttons[TransferSelectOptions.Util.SOURCE_TO_TARGET];
  this.buttons.targetToSourceButton = this.buttons[TransferSelectOptions.Util.TARGET_TO_SOURCE];
  this.buttons.parentForm = this.buttons.sourceToTargetButton.form;
  this.source = TransferSelectOptions.Util.getHTMLObject(this.sourceIdentifier, this.buttons.parentForm);
  this.target = TransferSelectOptions.Util.getHTMLObject(this.targetIdentifier, this.buttons.parentForm);

  this.bindUserValues();
  this.disabledColor = this.disabledAttributeColor;
  this.enabledColor = "#000";

  if (!TransferSelectOptions.Util.isValidBindRequest(this.source, this.target)) {
    var container = document.getElementById("tso" + this.currentInstance);
    container.parentNode.removeChild(container);
    return;
  }

  this.disableMovedOptions(this.source.options, this.target.options);
  this.buttons.sourceToTargetButton.currentInstance = this.currentInstance;
  this.buttons.targetToSourceButton.currentInstance = this.currentInstance;
  this.buttons.sourceToTargetButton.onclick = function() {
    instancesOfTransferSelectOptions[this.currentInstance].performMove(TransferSelectOptions.Util.SOURCE_TO_TARGET);
  };
  this.buttons.targetToSourceButton.onclick = function() {
    instancesOfTransferSelectOptions[this.currentInstance].performMove(TransferSelectOptions.Util.TARGET_TO_SOURCE);
  };

  if (this.selectTargetOnSubmit) {
	  FmasHelper.makeSelectOptionsSelectedOnFormSubmit(this.target, this.currentInstance);
  }
};

TransferSelectOptions.prototype.move = function(direction, toMoveList) {
  var isSourceToTargetTransfer = (direction == TransferSelectOptions.Util.SOURCE_TO_TARGET);
  var sourceList = isSourceToTargetTransfer ? this.source.options : this.target.options;
  var targetList = isSourceToTargetTransfer ? this.target.options : this.source.options;

  for (var i = 0; i < toMoveList.length; i++) {
    var alreadyExists = false;
    for (var j = 0; j < targetList.length; j++) {
      if (toMoveList[i].value == targetList[j].value) {
        alreadyExists = true;
        break;
      }
    }
    if (!alreadyExists && isSourceToTargetTransfer) {
      this.target.options[this.target.options.length] = new Option(toMoveList[i].text, toMoveList[i].value);
    }
  }

  if (isSourceToTargetTransfer) {
    this.disableMovedOptions(sourceList, toMoveList);
  } else {
    this.removeElements(sourceList, targetList, toMoveList);
  }
};

TransferSelectOptions.prototype.performMove = function(direction) {
  var isSourceToTargetTransfer = (direction == TransferSelectOptions.Util.SOURCE_TO_TARGET);
  var sourceList = isSourceToTargetTransfer ? this.source.options : this.target.options;
  var toMoveList = TransferSelectOptions.Util.getToMoveList(sourceList);

	this.move(direction, toMoveList);
};

TransferSelectOptions.prototype.removeElements = function(sourceOptions, targetOptions, toMoveOptions) {
  for (var j = 0; j < sourceOptions.length; j++) {
    for (var k = 0; k < toMoveOptions.length; k++) {
      if (sourceOptions[j].value == toMoveOptions[k].value) {
        for (var l = 0; l < targetOptions.length; l++) {
          if (sourceOptions[j].value == targetOptions[l].value) {
            targetOptions[l].style.color = this.enabledColor;
            break;
          }
        }
        sourceOptions[j].parentNode.removeChild(sourceOptions[j]);
      }
    }
  }
};

TransferSelectOptions.prototype.disableMovedOptions = function(sourceOptions, toMoveOptions) {
  for (var j = 0; j < sourceOptions.length; j++) {
    sourceOptions[j].selected = false;
    for (var k = 0; k < toMoveOptions.length; k++) {
      if (sourceOptions[j].value == toMoveOptions[k].value) {
        if (this.disabledAttributeCssClass != null) {
          sourceOptions[j].className = this.disabledAttributeCssClass;
        }
        sourceOptions[j].style.color = this.disabledColor;
        break;
      }
    }
  }
};

TransferSelectOptions.Util = {
  SOURCE_TO_TARGET : "sT",
  TARGET_TO_SOURCE : "tS",
  isValidBindRequest : function(source, target) {
    var isValid = false;
    if (
        typeof(source) != "undefined" &&
        typeof(target) != "undefined" &&
        source &&
        target &&
        source.form != null &&
        target.form != null &&
        source.tagName && source.tagName == "SELECT" && source.multiple == true &&
        target.tagName && target.tagName == "SELECT" && target.multiple == true
        ) {
      isValid = true;
    }
    return isValid;
  },
  getToMoveList : function(sourceOptions) {
    var toMoveList = new Array();
    for (var i = 0; i < sourceOptions.length; i++) {
      if (sourceOptions[i].selected && sourceOptions[i].style.display != 'none' && sourceOptions[i].style.visibility != 'hidden') {
        toMoveList[toMoveList.length] = {
          value: sourceOptions[i].value,
          text: sourceOptions[i].text
        };
      }
    }
    return toMoveList;
  },
  getTransferButtons : function(container) {
    var buttons = {};
    var buttonsInContainer = container.getElementsByTagName("button");
    if (buttonsInContainer.length > 0) {
      for (var i = 0; i < buttonsInContainer.length; i++) {
        if (buttonsInContainer[i].className == TransferSelectOptions.Util.SOURCE_TO_TARGET) {
          buttons[TransferSelectOptions.Util.SOURCE_TO_TARGET] = buttonsInContainer[i];
        } else {
          buttons[TransferSelectOptions.Util.TARGET_TO_SOURCE] = buttonsInContainer[i];
        }
      }
    }
    return buttons;
  },
  getHTMLObject : function(elementIdentifier, buttonForm) {
    if (!elementIdentifier || !buttonForm)return null;
    var hierarchy = elementIdentifier.split(".");
    var htmlObject = null;
    /*  if(hierarchy.length == 2){
     var requestedForm = document.forms[hierarchy[0]];
     htmlObject = requestedForm[hierarchy[1]];
     }else */
    if (buttonForm[elementIdentifier] && "SELECT" == buttonForm[elementIdentifier].tagName) {
      htmlObject = buttonForm[elementIdentifier];
    } else {
      htmlObject = FmasHelper.getById(elementIdentifier);
    }
    return htmlObject;
  }
};